package com.haoxi.gateway.main;

import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.haoxi.gateway.cmppmsg.CMPPCommon;
import com.haoxi.gateway.cmppmsg.CMPPConnectMessage;
import com.haoxi.gateway.cmppmsg.CMPPConnectRepMessage;
import com.haoxi.gateway.cmppmsg.CMPPSubmitMessage;
import com.haoxi.gateway.cmppmsg.CMPPSubmitRepMessage;
import com.haoxi.gateway.common.SQLConst;
import com.haoxi.gateway.common.SYSConst;
import com.haoxi.gateway.runner.RunnerMgr;
import com.haoxi.gateway.runner.smscommand.SubmitCmd;
import com.haoxi.gateway.util.db.DBConnectUtil;
import com.haoxi.gateway.util.db.DealOracleDBUtil;

public class test {
    private static Log log = LogFactory.getLog ( test.class );
    //private static String gw_ip = "192.168.1.3";
    private static String gw_ip = "211.139.215.133";
    private static int gw_port = 7890;
    private static String Source_Addr = "ZSJDTY";
    private static String shared_Secret = "zsJDty09";
    private static String service_id="TGD6919901";
    private static String mobile_num = "13822798764";
    
    
    public static void main(String[] args) {
        
        List list = new ArrayList();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        String dbName = "gateway";
        try {
            conn = DBConnectUtil.open(dbName);
            log.debug("dbname:" +dbName);
            //ps = conn.prepareStatement(SQLConst.GET_NEED_SEND_MT);
            ps = conn.prepareStatement("select * from deliver");
            rs = ps.executeQuery();
//            ps.setString(1, currentGateway);
//            ps.setInt(2, getMtSpeed);
           // ps = conn.prepareStatement(SQLConst.GET_NEED_SEND_MT_MySQL);
//            ps = conn.prepareStatement("insert into deliver(GATEWAYCODE,DESTMOBILE,ORGMOBILE,CONTENT,REG_DEL,MSG_FMT,SAVETIME,PROVINCE,CITYNUMBER) values(1,1,1,'我们','1','1',now(),'1','1')");
//            ps.executeUpdate();
//            //log.debug("GET_NEED_SEND_MT_MySQL:" +SQLConst.GET_NEED_SEND_MT_MySQL);
            SubmitCmd mt = null;
            int mtId = 0;
            String content;
            
            while (rs.next()) {
                mtId = rs.getInt("id");
                content = rs.getString("content");
                
                log.debug(mtId+":"+content);
                log.debug("utf8转码后："+(new String(content.getBytes("utf-8"),"unicode")));
                log.debug("8859转码后："+(new String(content.getBytes("iso-8859_1"),"unicode")));
                log.debug("转码后："+(new String(content.getBytes(),"unicode")));
            }
        }catch(Exception e){

            log.error(e.getMessage());
        }
        /**
        
        //建立Socket连接
        try{
            boolean isok=false;
            log.debug("开始Socket");
            Socket socket = new Socket(gw_ip,gw_port);
            log.debug("Socket success");
            CMPPConnectMessage message = new CMPPConnectMessage(Source_Addr,1,shared_Secret,new Date());
            log.debug("CMPPConnectMessage");
            message.setSequenceId(1);
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket
                    .getOutputStream()));
            
            out.write(message.getBytes());
            out.flush();
            DataInputStream in = new DataInputStream(new BufferedInputStream(socket
                    .getInputStream()));
            log.debug("out success");
            int msgLength = in.readInt();
            int commid = in.readInt();
            byte[] respbytes = new byte[msgLength - 8];
            in.readFully(respbytes);
            if (CMPPCommon.CMPP_CONNECT_RESP == commid) {
                CMPPConnectRepMessage loginResp = new CMPPConnectRepMessage(
                        respbytes);
                log.debug(loginResp);

                if (0 == loginResp.getStatus()) {
                    isok=true;
                    log.debug("<<<<<<bind to smc successfuly!");
                } else {
                    log.error("<<<<<<<<bind to smc fail");
                }
                log.debug("SequenceId="+loginResp.getSequenceId());
                log.debug("getVersion="+loginResp.getVersion());
                log.debug("getAuthenticatorISMG="+loginResp.getAuthenticatorISMG());
            }
            log.debug("start submit message");
            if(isok){
                String[] dest_terminal_id={mobile_num};
                byte[] content = "hello,welcome the test.孙程佳".getBytes();
                int msg_fmt = 15;
                log.debug("开始组装submit message");
                CMPPSubmitMessage submit = new CMPPSubmitMessage(1, 1,0, 1, service_id, 0, mobile_num, 0, 0, msg_fmt, "999999", "01", "0", null, null, "106573450968", dest_terminal_id, content, "yes");
                log.debug(submit);
                log.debug("组装完毕,开始写入socket");
                out.write((submit.getBytes()));
                out.flush();
                log.debug("写入完毕");
                msgLength=in.readInt();
                log.debug("msgLength:"+msgLength);
                commid=in.readInt();
                log.debug("commid:"+commid);
                respbytes=new byte[msgLength-8];
                in.readFully(respbytes);
                log.debug("respbytes.length:"+respbytes.length);
  
                if(CMPPCommon.CMPP_SUBMIT_RESP==commid){
                    CMPPSubmitRepMessage submitRes = new CMPPSubmitRepMessage(respbytes);
                    log.debug(submitRes.toString());
                    if(0==submitRes.getResult()){
                        
                        log.debug("submit message is ok");
                    }
                    else{
                        
                        log.debug("submit message is fail:" + submitRes.getResult());
                    }
                }
                }
            
        }catch(Exception e) {
            log.debug(e.getMessage());
        }
        */
        //发送短信

        
        
        //接收反馈
        
        //接收MO
    }
    
    
    static int bytes2int(byte[] b)
    {
        
        
             //byte[] b=new byte[]{1,2,3,4}; 
             int mask=0xff;
             int temp=0;
            int res=0;
            for(int i=0;i<4;i++){
                res<<=8;
                temp=b[i]&mask;
                res|=temp;
            }
           return res;
    } 

    static byte[] int2bytes(int num)
    {
           byte[] b=new byte[4];
           int mask=0xff;
           for(int i=0;i<4;i++){
                b[i]=(byte)(num>>>(24-i*8));
           }
          return b;
    }

}
